import sys
import time
import pygame
import random

pygame.init()
screen = pygame.display.set_mode((500, 750))
pygame.display.set_caption("飞行棋")

# 棋盘上边界
y_top = 100
# 棋盘下边界
y_bottom = 600
# 颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (248, 65, 47)
yellow = (248, 212, 2)
green = (62, 207, 140)
blue = (6, 183, 227)

color = [red, yellow, blue, green]

# 红色棋子坐标
red_chess = [(46.2, 407.4 + y_top), (46.2, 447.4 + y_top), (86.2, 407.4 + y_top), (86.2, 447.4 + y_top)]
# 黄色棋子坐标
yellow_chess = [(46.2, 40 + y_top), (46.2, 80 + y_top), (86.2, 40 + y_top), (86.2, 80 + y_top)]
# 蓝色棋子坐标
blue_chess = [(454.4, 40 + y_top), (454.4, 80 + y_top), (414.4, 40 + y_top), (414.4, 80 + y_top)]
# 绿色棋子坐标
green_chess = [(455, 447.4 + y_top), (455, 407.4 + y_top), (415, 447.4 + y_top), (415, 407.4 + y_top)]

# 棋子
chess = [
    [
        [46.2, 407.4 + y_top, False, False],
        [46.2, 447.4 + y_top, False, False],
        [86.2, 407.4 + y_top, False, False],
        [86.2, 447.4 + y_top, False, False]
    ],
    [
        [46.2, 40 + y_top, False, False],
        [46.2, 80 + y_top, False, False],
        [86.2, 40 + y_top, False, False],
        [86.2, 80 + y_top, False, False]
    ],
    [
        [454.4, 40 + y_top, False, False],
        [454.4, 80 + y_top, False, False],
        [414.4, 40 + y_top, False, False],
        [414.4, 80 + y_top, False, False]
    ],
    [
        [455, 447.4 + y_top, False, False],
        [455, 407.4 + y_top, False, False],
        [415, 447.4 + y_top, False, False],
        [415, 407.4 + y_top, False, False]
    ]
]


# 原始坐标
row_chess_pos = [red_chess, yellow_chess, blue_chess, green_chess]

# 起飞点坐标
start_pos = [(128.2, 461.4 + y_top), (33.2, 122 + y_top), (372.4, 26 + y_top), (469, 365.4 + y_top)]

# 门口第一个格子的坐标
start_pos_first = [(157.6, 445.2 + y_top), (49.4, 151.4 + y_top), (342, 43 + y_top), (451.4, 335.8 + y_top)]

# 棋盘格子坐标（4， 2）（5，3）（17(139.4)， 3）（18， 0）（30，0）（31(360.6)，1）（43，1）（44， 2）52格
grids = [(157.6, 445.2 + y_top), (149.6, 415 + y_top), (149.6, 383.6 + y_top), (157.6, 354.4 + y_top),
         (139, 336.4 + y_top), (109.8, 343.8 + y_top), (78.4, 343.8 + y_top), (49.2, 335.8 + y_top),
         (41.2, 306.6 + y_top), (41.2, 275.2 + y_top), (41.2, 243.8 + y_top), (41.2, 212.4 + y_top),
         (41.2, 181 + y_top), (49.4, 151.4 + y_top), (78.8, 143.4 + y_top), (110.2, 143.4 + y_top),
         (139, 151.4 + y_top), (158, 132.8 + y_top), (150, 103.6 + y_top), (150, 72.4 + y_top), (158, 43 + y_top),
         (187.2, 35 + y_top), (218.6, 35 + y_top), (250, 35 + y_top), (281.4, 35 + y_top), (312.8, 35 + y_top),
         (342, 43 + y_top), (350, 72.2 + y_top), (350, 103.6 + y_top), (342, 132.8 + y_top), (361.6, 151.4 + y_top),
         (389.8, 143.4 + y_top), (421.2, 143.4 + y_top), (451.4, 151.4 + y_top), (459.4, 181 + y_top),
         (459.4, 212.4 + y_top), (459.4, 243.8 + y_top), (459.4, 275.2 + y_top), (459.4, 306.6 + y_top),
         (451.4, 335.8 + y_top), (422.2, 343.8 + y_top), (390.8, 343.8 + y_top), (361.6, 335.8 + y_top),
         (343, 354.4 + y_top), (351, 383.6 + y_top), (351, 415 + y_top), (343, 444.2 + y_top), (313.8, 452.4 + y_top),
         (282.4, 452.4 + y_top), (250, 452.4 + y_top), (219.4, 452.4 + y_top), (187.8, 452.4 + y_top)]

# 降落格子坐标（49，10，23，36）
red_grids = [(250, 452.4 + y_top), (250, 415.2 + y_top), (250, 384.8 + y_top), (250, 353.4 + y_top), (250, 322 + y_top),
             (250, 290.6 + y_top), (250, 264.6 + y_top)]
yellow_grids = [(41.2, 243.8 + y_top), (78.4, 243.8 + y_top), (109.8, 243.8 + y_top), (141.2, 243.8 + y_top),
                (172.6, 243.8 + y_top), (204, 243.8 + y_top), (230, 243.8 + y_top)]
blue_grids = [(250, 35 + y_top), (250, 72.2 + y_top), (250, 103.6 + y_top), (250, 135.2 + y_top),
              (250, 166.6 + y_top), (250, 197.8 + y_top), (250, 223.8 + y_top)]
green_grids = [(459.4, 243.8 + y_top), (420.6, 243.8 + y_top), (389.2, 243.8 + y_top), (357.8, 243.8 + y_top), (326.4, 243.8 + y_top),
               (295, 243.8 + y_top), (269, 243.8 + y_top)]

end_grids = [red_grids, yellow_grids, blue_grids, green_grids]

# 计数器
red_counter = 0
yellow_counter = 0
blue_counter = 0
green_counter = 0
counter = [red_counter, yellow_counter, blue_counter, green_counter]

# 游戏轮数
game_round = 0

# 骰子点数
dice = 6

# 阶段(0为掷骰子阶段,1为选择和移动棋子阶段)
stage = 0

# 掷骰子标记
dice_flag = False

# 字体
font = pygame.font.SysFont("simhei", 10, bold=True)
num_font = pygame.font.SysFont("simhei", 80)
dice_font = pygame.font.SysFont("simhei", 30)
mess_font = pygame.font.SysFont("simhei", 20)
arrived_font = pygame.font.SysFont("simhei", 20, bold=True)

# 创建一个时钟对象
clock = pygame.time.Clock()


def draw_dice():
    global dice
    global stage
    global game_round
    global dice_flag
    dice = random.randint(1, 6)
    # 加入保底机制(当一个玩家连续5个回合没有飞机起飞且没有掷到6，强制变成6)
    for i in range(4):
        if chess[game_round % 4][i][2]:
            break
        if i == 3:
            if dice != 6:
                counter[game_round % 4] += 1
            else:
                counter[game_round % 4] = 0
            if counter[game_round % 4] == 5:
                dice = 6
    for m in range(10):
        for n in range(1, 7):
            dice_flag = True
            draw_board()
            text = num_font.render(str(n), True, black)
            screen.blit(text, (300, 600))
            if m == 9 and n == dice:
                break
            pygame.display.flip()
            time.sleep(0.03)
    dice_flag = False
    if dice == 6:
        stage = 1
    else:
        now_chess = chess[game_round % 4]
        for s in range(len(chess)):
            if now_chess[s][2]:
                stage = 1
                break
        else:
            game_round += 1


def choose_chess(pos):
    x, y = pos
    color_index = game_round % 4
    # 判断玩家是否正确地选中了要移动的棋子
    for j in range(len(chess[color_index])):
        if chess[color_index][j][0] - 12 <= x <= chess[color_index][j][0] + 12 and\
                chess[color_index][j][1] - 12 <= y <= chess[color_index][j][1] + 12:
            if (chess[color_index][j][2] is False and dice == 6) or (chess[color_index][j][2] is True):
                return j
            break


def draw_board():
    screen.fill(white)
    if game_round % 4 == 0:
        action_person = mess_font.render("当前回合玩家: 红色方", True, black)
    elif game_round % 4 == 1:
        action_person = mess_font.render("当前回合玩家: 黄色方", True, black)
    elif game_round % 4 == 2:
        action_person = mess_font.render("当前回合玩家: 蓝色方", True, black)
    else:
        action_person = mess_font.render("当前回合玩家: 绿色方", True, black)
    screen.blit(action_person, (0, 0))
    pygame.draw.rect(screen, (1, 57, 92), (0, 100, 500, 500))

    pygame.draw.line(screen, (0, 0, 0), (0, y_top), (500, y_top))
    # 红色起飞场
    pygame.draw.rect(screen, red, (21.2, 382.4 + y_top, 90, 90))
    pygame.draw.circle(screen, white, (46.2, 407.4 + y_top), 15)
    pygame.draw.circle(screen, white, (46.2, 447.4 + y_top), 15)
    pygame.draw.circle(screen, white, (86.2, 407.4 + y_top), 15)
    pygame.draw.circle(screen, white, (86.2, 447.4 + y_top), 15)
    # 起飞区域
    pygame.draw.polygon(screen, red, [(116.2, 442.4 + y_top), (127.2, 442.4 + y_top), (158.2, 472.4 + y_top),
                                      (116.2, 472.4 + y_top)])
    pygame.draw.polygon(screen, white, [(118.2, 450.4 + y_top), (122.7, 445.4 + y_top), (127.2, 450.4 + y_top)])
    pygame.draw.polygon(screen, red, [(120.2, 450.4 + y_top), (122.7, 448.4 + y_top), (124.2, 450.4 + y_top)])
    pygame.draw.polygon(screen, white, [(118.2, 455.4 + y_top), (122.7, 450.4 + y_top), (127.2, 455.4 + y_top)])
    pygame.draw.polygon(screen, red, [(120.2, 455.4 + y_top), (122.7, 453.4 + y_top), (124.2, 455.4 + y_top)])
    text = font.render("起飞点", True, white)
    screen.blit(text, (116.2, 460.4 + y_top))
    # 黄色起飞场
    pygame.draw.rect(screen, yellow, (21.2, 15 + y_top, 90, 90))
    pygame.draw.circle(screen, white, (46.2, 40 + y_top), 15)
    pygame.draw.circle(screen, white, (46.2, 80 + y_top), 15)
    pygame.draw.circle(screen, white, (86.2, 40 + y_top), 15)
    pygame.draw.circle(screen, white, (86.2, 80 + y_top), 15)
    # 起飞区域
    pygame.draw.polygon(screen, yellow, [(51.2, 110 + y_top), (51.2, 121 + y_top), (21.2, 152 + y_top),
                                         (21.2, 110 + y_top)])
    pygame.draw.polygon(screen, white, [(43.2, 112 + y_top), (48.2, 116.5 + y_top), (43.2, 121 + y_top)])
    pygame.draw.polygon(screen, yellow, [(43.2, 114.5 + y_top), (45.2, 116.5 + y_top), (43.2, 119 + y_top)])
    pygame.draw.polygon(screen, white, [(38.2, 112 + y_top), (43.2, 116.5 + y_top), (38.2, 121 + y_top)])
    pygame.draw.polygon(screen, yellow, [(38.2, 114 + y_top), (40.2, 116.5 + y_top), (38.2, 119 + y_top)])
    screen.blit(pygame.transform.rotate(text, 270), (23.2, 111 + y_top))
    # 蓝色起飞场
    pygame.draw.rect(screen, blue, (389.4, 15 + y_top, 90, 90))
    pygame.draw.circle(screen, white, (454.4, 40 + y_top), 15)
    pygame.draw.circle(screen, white, (454.4, 80 + y_top), 15)
    pygame.draw.circle(screen, white, (414.4, 40 + y_top), 15)
    pygame.draw.circle(screen, white, (414.4, 80 + y_top), 15)
    # 起飞区域
    pygame.draw.polygon(screen, blue, [(384.4, 45 + y_top), (373.4, 45 + y_top), (342.4, 15 + y_top),
                                       (384.4, 15 + y_top)])
    pygame.draw.polygon(screen, white, [(382.4, 37 + y_top), (377.9, 42 + y_top), (373.4, 37 + y_top)])
    pygame.draw.polygon(screen, blue, [(380.4, 37 + y_top), (377.9, 39 + y_top), (375.4, 37 + y_top)])
    pygame.draw.polygon(screen, white, [(382.4, 32 + y_top), (377.9, 37 + y_top), (373.4, 32 + y_top)])
    pygame.draw.polygon(screen, blue, [(380.4, 32 + y_top), (377.9, 34 + y_top), (375.4, 32 + y_top)])
    screen.blit(pygame.transform.rotate(text, 180), (352.4, 17 + y_top))
    # 绿色起飞场
    pygame.draw.rect(screen, green, (390, 382.4 + y_top, 90, 90))
    pygame.draw.circle(screen, white, (455, 447.4 + y_top), 15)
    pygame.draw.circle(screen, white, (455, 407.4 + y_top), 15)
    pygame.draw.circle(screen, white, (415, 447.4 + y_top), 15)
    pygame.draw.circle(screen, white, (415, 407.4 + y_top), 15)
    # 起飞区域
    pygame.draw.polygon(screen, green, [(450, 377.4 + y_top), (450, 366.4 + y_top), (480, 335.4 + y_top),
                                        (480, 377.4 + y_top)])
    pygame.draw.polygon(screen, white, [(460, 375.4 + y_top), (455, 370.9 + y_top), (460, 365.9 + y_top)])
    pygame.draw.polygon(screen, green, [(460, 373.4 + y_top), (458, 370.9 + y_top), (460, 367.9 + y_top)])
    pygame.draw.polygon(screen, white, [(465, 375.4 + y_top), (460, 370.9 + y_top), (465, 365.9 + y_top)])
    pygame.draw.polygon(screen, green, [(465, 373.4 + y_top), (463, 370.9 + y_top), (465, 367.9 + y_top)])
    screen.blit(pygame.transform.rotate(text, 90), (469, 347.4 + y_top))
    # 棋盘
    pygame.draw.polygon(screen, green, [(129.6, 432.2 + y_top), (169.4, 432.2 + y_top), (169.4, 472.2 + y_top)])
    pygame.draw.rect(screen, red, (129.6, 400.8 + y_top, 40, 28.4))
    pygame.draw.rect(screen, yellow, (129.6, 369.4 + y_top, 40, 28.4))
    pygame.draw.polygon(screen, blue, [(169.6, 326.4 + y_top), (169.6, 366.4 + y_top), (129.6, 366.4 + y_top)])
    pygame.draw.polygon(screen, green, [(127, 363.8 + y_top), (127, 323.8 + y_top), (167, 323.8 + y_top)])
    pygame.draw.rect(screen, red, (95.6, 323.8 + y_top, 28.4, 40))
    pygame.draw.rect(screen, yellow, (64.2, 323.8 + y_top, 28.4, 40))
    pygame.draw.polygon(screen, blue, [(21.2, 323.8 + y_top), (61.2, 323.8 + y_top), (61.2, 363.8 + y_top)])

    pygame.draw.rect(screen, green, (21.2, 292.4 + y_top, 40, 28.4))
    pygame.draw.rect(screen, red, (21.2, 261 + y_top, 40, 28.4))
    pygame.draw.rect(screen, yellow, (21.2, 229.6 + y_top, 40, 28.4))
    pygame.draw.polygon(screen, white, [(42.2, 235.6 + y_top), (53.2, 243.8 + y_top), (42.2, 252 + y_top)])
    pygame.draw.rect(screen, white, (34.2, 240.6 + y_top, 8, 7))
    pygame.draw.rect(screen, white, (27.2, 240.6 + y_top, 7, 13))
    pygame.draw.rect(screen, blue, (21.2, 198.2 + y_top, 40, 28.4))
    pygame.draw.rect(screen, green, (21.2, 166.8 + y_top, 40, 28.4))

    pygame.draw.polygon(screen, red, [(61.2, 123.4 + y_top), (61.2, 163.4 + y_top), (21.2, 163.4 + y_top)])
    pygame.draw.rect(screen, yellow, (64.6, 123.4 + y_top, 28.4, 40))
    pygame.draw.rect(screen, blue, (96, 123.4 + y_top, 28.4, 40))
    pygame.draw.polygon(screen, green, [(127.4, 123.4 + y_top), (127.4, 163.4 + y_top), (167.4, 163.4 + y_top)])
    pygame.draw.polygon(screen, red, [(130, 120.8 + y_top), (170, 120.8 + y_top), (170, 160.8 + y_top)])
    pygame.draw.rect(screen, yellow, (130, 89.4 + y_top, 40, 28.4))
    pygame.draw.rect(screen, blue, (130, 58 + y_top, 40, 28.4))
    pygame.draw.polygon(screen, green, [(130, 55 + y_top), (170, 55 + y_top), (170, 15 + y_top)])

    pygame.draw.rect(screen, red, (173, 15 + y_top, 28.4, 40))
    pygame.draw.rect(screen, yellow, (204.4, 15 + y_top, 28.4, 40))
    pygame.draw.rect(screen, blue, (235.8, 15 + y_top, 28.4, 40))
    pygame.draw.polygon(screen, white, [(241.8, 38 + y_top), (250, 49 + y_top), (259.2, 38 + y_top)])
    pygame.draw.rect(screen, white, (247.8, 30 + y_top, 7, 8))
    pygame.draw.rect(screen, white, (241.8, 23 + y_top, 13, 7))
    pygame.draw.rect(screen, green, (267.2, 15 + y_top, 28.4, 40))
    pygame.draw.rect(screen, red, (298.6, 15 + y_top, 28.4, 40))

    pygame.draw.polygon(screen, yellow, [(370, 55 + y_top), (330, 55 + y_top), (330, 15 + y_top)])
    pygame.draw.rect(screen, blue, (330, 58 + y_top, 40, 28.4))
    pygame.draw.rect(screen, green, (330, 89.4 + y_top, 40, 28.4))
    pygame.draw.polygon(screen, red, [(370, 120.8 + y_top), (330, 120.8 + y_top), (330, 160.8 + y_top)])
    pygame.draw.polygon(screen, yellow, [(332.6, 163.4 + y_top), (372.6, 163.4 + y_top), (372.6, 123.4 + y_top)])
    pygame.draw.rect(screen, blue, (375.6, 123.4 + y_top, 28.4, 40))
    pygame.draw.rect(screen, green, (407, 123.4 + y_top, 28.4, 40))
    pygame.draw.polygon(screen, red, [(439.4, 123.4 + y_top), (439.4, 163.4 + y_top), (479.4, 163.4 + y_top)])

    pygame.draw.rect(screen, yellow, (439.4, 166.8 + y_top, 40, 28.4))
    pygame.draw.rect(screen, blue, (439.4, 198.2 + y_top, 40, 28.4))
    pygame.draw.rect(screen, green, (439.4, 229.6 + y_top, 40, 28.4))
    pygame.draw.polygon(screen, white, [(457.6, 235.6 + y_top), (446.6, 243.8 + y_top), (457.6, 252 + y_top)])
    pygame.draw.rect(screen, white, (457.6, 240.6 + y_top, 8, 7))
    pygame.draw.rect(screen, white, (465.6, 234.6 + y_top, 7, 13))
    pygame.draw.rect(screen, red, (439.4, 261 + y_top, 40, 28.4))
    pygame.draw.rect(screen, yellow, (439.4, 292.4 + y_top, 40, 28.4))

    pygame.draw.polygon(screen, blue, [(439.4, 323.8 + y_top), (439.4, 363.8 + y_top), (479, 323.8 + y_top)])
    pygame.draw.rect(screen, green, (408, 323.8 + y_top, 28.4, 40))
    pygame.draw.rect(screen, red, (376.6, 323.8 + y_top, 28.4, 40))
    pygame.draw.polygon(screen, yellow, [(333.6, 323.8 + y_top), (373.6, 323.8 + y_top), (373.6, 363.8 + y_top)])
    pygame.draw.polygon(screen, blue, [(331, 326.4 + y_top), (331, 366.4 + y_top), (371, 366.4 + y_top)])
    pygame.draw.rect(screen, green, (331, 369.4 + y_top, 40, 28.4))
    pygame.draw.rect(screen, red, (331, 400.8 + y_top, 40, 28.4))
    pygame.draw.polygon(screen, yellow, [(371, 432.2 + y_top), (331, 432.2 + y_top), (331, 472.2 + y_top)])

    pygame.draw.rect(screen, blue, (299.6, 432.4 + y_top, 28.4, 40))
    pygame.draw.rect(screen, green, (268.2, 432.4 + y_top, 28.4, 40))
    pygame.draw.rect(screen, red, (236.8, 432.4 + y_top, 28.4, 40))
    pygame.draw.polygon(screen, white, [(241.8, 448.4 + y_top), (250, 437.4 + y_top), (259.2, 448.4 + y_top)])
    pygame.draw.rect(screen, white, (247.8, 448.4 + y_top, 7, 8))
    pygame.draw.rect(screen, white, (247.8, 456.4 + y_top, 13, 7))
    pygame.draw.rect(screen, yellow, (205.2, 432.4 + y_top, 28.4, 40))
    pygame.draw.rect(screen, blue, (173.6, 432.4 + y_top, 28.4, 40))

    pygame.draw.rect(screen, red, (236.8, 401 + y_top, 28.4, 28.4))
    pygame.draw.rect(screen, red, (236.8, 370.6 + y_top, 28.4, 28.4))
    pygame.draw.rect(screen, blue, (173, 339.2 + y_top, 155, 25))
    pygame.draw.polygon(screen, white, [(201, 341.2 + y_top), (192, 351.2 + y_top), (201, 362.2 + y_top)])
    pygame.draw.polygon(screen, blue, [(206, 341.2 + y_top), (197, 351.2 + y_top), (206, 362.2 + y_top)])
    pygame.draw.polygon(screen, white, [(213, 341.2 + y_top), (204, 351.2 + y_top), (213, 362.2 + y_top)])
    pygame.draw.polygon(screen, blue, [(218, 341.2 + y_top), (209, 351.2 + y_top), (218, 362.2 + y_top)])
    pygame.draw.polygon(screen, white, [(293, 341.2 + y_top), (284, 351.2 + y_top), (293, 362.2 + y_top)])
    pygame.draw.polygon(screen, blue, [(298, 341.2 + y_top), (289, 351.2 + y_top), (298, 362.2 + y_top)])
    pygame.draw.polygon(screen, white, [(305, 341.2 + y_top), (296, 351.2 + y_top), (305, 362.2 + y_top)])
    pygame.draw.polygon(screen, blue, [(310, 341.2 + y_top), (301, 351.2 + y_top), (310, 362.2 + y_top)])
    pygame.draw.rect(screen, red, (236.8, 339.2 + y_top, 28.4, 28.4))
    pygame.draw.circle(screen, white, (250, 353.4 + y_top), 12)
    pygame.draw.rect(screen, red, (236.8, 307.8 + y_top, 28.4, 28.4))
    pygame.draw.rect(screen, red, (236.8, 276.4 + y_top, 28.4, 28.4))
    pygame.draw.polygon(screen, red, [(218.2, 276.4 + y_top), (250, 241.8 + y_top), (280.8, 276.4 + y_top)])

    pygame.draw.rect(screen, yellow, (64.2, 229.6 + y_top, 28.4, 28.4))
    pygame.draw.rect(screen, yellow, (95.6, 229.6 + y_top, 28.4, 28.4))
    pygame.draw.rect(screen, green, (128.4, 166.4 + y_top, 25, 155))
    pygame.draw.polygon(screen, white, [(130.4, 192.4 + y_top), (140.4, 183.4 + y_top), (151.4, 192.4 + y_top)])
    pygame.draw.polygon(screen, green, [(135.4, 192.4 + y_top), (140.4, 188.4 + y_top), (145.4, 192.4 + y_top)])
    pygame.draw.polygon(screen, white, [(130.4, 204.4 + y_top), (140.4, 195.4 + y_top), (151.4, 204.4 + y_top)])
    pygame.draw.polygon(screen, green, [(135.4, 204.4 + y_top), (140.4, 200.4 + y_top), (145.4, 204.4 + y_top)])
    pygame.draw.polygon(screen, white, [(130.4, 284.4 + y_top), (140.4, 275.4 + y_top), (151.4, 284.4 + y_top)])
    pygame.draw.polygon(screen, green, [(135.4, 284.4 + y_top), (140.4, 280.4 + y_top), (145.4, 284.4 + y_top)])
    pygame.draw.polygon(screen, white, [(130.4, 296.4 + y_top), (140.4, 287.4 + y_top), (151.4, 296.4 + y_top)])
    pygame.draw.polygon(screen, green, [(135.4, 296.4 + y_top), (140.4, 292.4 + y_top), (145.4, 296.4 + y_top)])
    pygame.draw.rect(screen, yellow, (127, 229.6 + y_top, 28.4, 28.4))
    pygame.draw.circle(screen, white, (141.4, 243 + y_top), 12)
    pygame.draw.rect(screen, yellow, (158.4, 229.6 + y_top, 28.4, 28.4))
    pygame.draw.rect(screen, yellow, (189.8, 229.6 + y_top, 28.4, 28.4))
    pygame.draw.polygon(screen, yellow, [(217.4, 276.4 + y_top), (250, 241.8 + y_top), (218.2, 210.4 + y_top)])

    pygame.draw.rect(screen, blue, (235.8, 58 + y_top, 28.4, 28.4))
    pygame.draw.rect(screen, blue, (235.8, 89.4 + y_top, 28.4, 28.4))
    pygame.draw.rect(screen, red, (173, 122 + y_top, 155, 25))
    pygame.draw.polygon(screen, white, [(192, 123 + y_top), (201, 133 + y_top), (192, 143 + y_top)])
    pygame.draw.polygon(screen, red, [(192, 128 + y_top), (197, 133 + y_top), (192, 138 + y_top)])
    pygame.draw.polygon(screen, white, [(204, 123 + y_top), (213, 133 + y_top), (204, 143 + y_top)])
    pygame.draw.polygon(screen, red, [(204, 128 + y_top), (209, 133 + y_top), (204, 138 + y_top)])
    pygame.draw.polygon(screen, white, [(284, 123 + y_top), (293, 133 + y_top), (284, 143 + y_top)])
    pygame.draw.polygon(screen, red, [(284, 128 + y_top), (289, 133 + y_top), (284, 138 + y_top)])
    pygame.draw.polygon(screen, white, [(296, 123 + y_top), (305, 133 + y_top), (296, 143 + y_top)])
    pygame.draw.polygon(screen, red, [(296, 128 + y_top), (301, 133 + y_top), (296, 138 + y_top)])
    pygame.draw.rect(screen, blue, (235.8, 120.8 + y_top, 28.4, 28.4))
    pygame.draw.circle(screen, white, (250, 135.2 + y_top), 12)
    pygame.draw.rect(screen, blue, (235.8, 151.4 + y_top, 28.4, 28.4))
    pygame.draw.rect(screen, blue, (235.8, 182 + y_top, 28.4, 28.4))
    pygame.draw.polygon(screen, blue, [(218.2, 210.4 + y_top), (250, 241.8 + y_top), (280.8, 210.4 + y_top)])

    pygame.draw.rect(screen, green, (406.4, 229.6 + y_top, 28.4, 28.4))
    pygame.draw.rect(screen, green, (375, 229.6 + y_top, 28.4, 28.4))
    pygame.draw.rect(screen, yellow, (344.6, 166.4 + y_top, 25, 155))
    pygame.draw.polygon(screen, white, [(346.4, 183.4 + y_top), (356.4, 192.4 + y_top), (367.4, 183.4 + y_top)])
    pygame.draw.polygon(screen, yellow, [(351.4, 183.4 + y_top), (356.4, 188.4 + y_top), (361.4, 183.4 + y_top)])
    pygame.draw.polygon(screen, white, [(346.4, 195.4 + y_top), (356.4, 204.4 + y_top), (367.4, 195.4 + y_top)])
    pygame.draw.polygon(screen, yellow, [(351.4, 195.4 + y_top), (356.4, 200.4 + y_top), (361.4, 195.4 + y_top)])
    pygame.draw.polygon(screen, white, [(346.4, 275.4 + y_top), (356.4, 284.4 + y_top), (367.4, 275.4 + y_top)])
    pygame.draw.polygon(screen, yellow, [(351.4, 275.4 + y_top), (356.4, 280.4 + y_top), (361.4, 275.4 + y_top)])
    pygame.draw.polygon(screen, white, [(346.4, 287.4 + y_top), (356.4, 296.4 + y_top), (367.4, 287.4 + y_top)])
    pygame.draw.polygon(screen, yellow, [(351.4, 287.4 + y_top), (356.4, 292.4 + y_top), (361.4, 287.4 + y_top)])
    pygame.draw.rect(screen, green, (343.6, 229.6 + y_top, 28.4, 28.4))
    pygame.draw.circle(screen, white, (357, 244 + y_top), 12)
    pygame.draw.rect(screen, green, (312.2, 229.6 + y_top, 28.4, 28.4))
    pygame.draw.rect(screen, green, (280.8, 229.6 + y_top, 28.4, 28.4))
    pygame.draw.polygon(screen, green, [(280.8, 276.4 + y_top), (250, 241.8 + y_top), (280.8, 210.4 + y_top)])

    # 绘制棋子
    for i in range(len(start_pos)):
        pygame.draw.circle(screen, black, start_pos[i], 12)
    for i in range(len(grids)):
        pygame.draw.circle(screen, black, grids[i], 12)
    for i in range(len(red_grids)):
        pygame.draw.circle(screen, black, red_grids[i], 12)
    for i in range(len(yellow_grids)):
        pygame.draw.circle(screen, black, yellow_grids[i], 12)
    for i in range(len(blue_grids)):
        pygame.draw.circle(screen, black, blue_grids[i], 12)
    for i in range(len(green_grids)):
        pygame.draw.circle(screen, black, green_grids[i], 12)
    for i in range(len(chess)):
        if i != game_round % 4:
            for p in range(len(chess[i])):
                if i == 0:
                    if chess[i][p][3]:
                        text = arrived_font.render("进", True, black)
                        screen.blit(text, (chess[i][p][0] - text.get_width() / 2,
                                           chess[i][p][1] - text.get_height() / 2))
                    else:
                        pygame.draw.circle(screen, red, (chess[i][p][0], chess[i][p][1]), 12)
                elif i == 1:
                    if chess[i][p][3]:
                        text = arrived_font.render("进", True, black)
                        screen.blit(pygame.transform.rotate(text, 270), (chess[i][p][0] - text.get_width() / 2,
                                           chess[i][p][1] - text.get_height() / 2))
                    else:
                        pygame.draw.circle(screen, yellow, (chess[i][p][0], chess[i][p][1]), 12)
                elif i == 2:
                    if chess[i][p][3]:
                        text = arrived_font.render("进", True, black)
                        screen.blit(pygame.transform.rotate(text, 180), (chess[i][p][0] - text.get_width() / 2,
                                           chess[i][p][1] - text.get_height() / 2))
                    else:
                        pygame.draw.circle(screen, blue, (chess[i][p][0], chess[i][p][1]), 12)
                elif i == 3:
                    if chess[i][p][3]:
                        text = arrived_font.render("进", True, black)
                        screen.blit(pygame.transform.rotate(text, 90), (chess[i][p][0] - text.get_width() / 2,
                                           chess[i][p][1] - text.get_height() / 2))
                    else:
                        pygame.draw.circle(screen, green, (chess[i][p][0], chess[i][p][1]), 12)
    for i in range(4):
        if game_round % 4 == 0:
            if chess[game_round % 4][i][3]:
                text = arrived_font.render("进", True, black)
                screen.blit(text, (chess[game_round % 4][i][0] - text.get_width() / 2,
                                   chess[game_round % 4][i][1] - text.get_height() / 2))
            else:
                pygame.draw.circle(screen, red, (chess[game_round % 4][i][0], chess[game_round % 4][i][1]), 12)
        elif game_round % 4 == 1:
            if chess[game_round % 4][i][3]:
                text = arrived_font.render("进", True, black)
                screen.blit(text, (chess[game_round % 4][i][0] - text.get_width() / 2,
                                   chess[game_round % 4][i][1] - text.get_height() / 2))
            else:
                pygame.draw.circle(screen, yellow, (chess[game_round % 4][i][0], chess[game_round % 4][i][1]), 12)
        elif game_round % 4 == 2:
            if chess[game_round % 4][i][3]:
                text = arrived_font.render("进", True, black)
                screen.blit(text, (chess[game_round % 4][i][0] - text.get_width() / 2,
                                   chess[game_round % 4][i][1] - text.get_height() / 2))
            else:
                pygame.draw.circle(screen, blue, (chess[game_round % 4][i][0], chess[game_round % 4][i][1]), 12)
        elif game_round % 4 == 3:
            if chess[game_round % 4][i][3]:
                text = arrived_font.render("进", True, black)
                screen.blit(text, (chess[game_round % 4][i][0] - text.get_width() / 2,
                                   chess[game_round % 4][i][1] - text.get_height() / 2))
            else:
                pygame.draw.circle(screen, green, (chess[game_round % 4][i][0], chess[game_round % 4][i][1]), 12)

    # 掷骰子按钮
    if stage == 0:
        pygame.draw.rect(screen, color[game_round % 4], (50, 630, 100, 50), 5)
        text = dice_font.render("掷骰子", True, color[game_round % 4])
        screen.blit(text, (55, 640))
    # 骰子点数显示框
    pygame.draw.rect(screen, color[game_round % 4], (270, 600, 100, 100), 5)
    # 绘制骰子点数
    if not dice_flag:
        text = num_font.render(str(dice), True, black)
        screen.blit(text, (300, 600))


def move_chess(j):
    """移动棋子"""
    """
    参数：
    j: 棋子的序数
    返回值：
    None
    """
    global stage
    global game_round
    color = game_round % 4
    moved_chess = chess[color]
    # 判断棋子是否起飞
    if moved_chess[j][2]:
        # 判断棋子是否在终点格子中
        if (moved_chess[j][0], moved_chess[j][1]) in end_grids[color]:
            grid_index = end_grids[color].index((moved_chess[j][0], moved_chess[j][1]))
            for k in range(grid_index + 1, grid_index + dice + 1):
                # 当索引k超过终点格子数时，索引k取12 - k
                if k > 6:
                    k = 12 - k
                x = (moved_chess[j][0] - end_grids[color][k][0]) / 35
                y = (moved_chess[j][1] - end_grids[color][k][1]) / 35
                while moved_chess[j][0] != end_grids[color][k][0] or moved_chess[j][1] != end_grids[color][k][1]:
                    if (-1 <= moved_chess[j][0] - end_grids[color][k][0] <= 1 and x != 0) or (
                            -1 <= moved_chess[j][1] - end_grids[color][k][1] <= 1 and y != 0):
                        moved_chess[j][0] = end_grids[color][k][0]
                        moved_chess[j][1] = end_grids[color][k][1]
                    else:
                        moved_chess[j][0] = moved_chess[j][0] - x
                        moved_chess[j][1] = moved_chess[j][1] - y
                    draw_board()
                    pygame.display.flip()
                    time.sleep(0.02)
        # 判断棋子是否在正常的格子中
        elif (moved_chess[j][0], moved_chess[j][1]) in grids:
            grid_index = grids.index((moved_chess[j][0], moved_chess[j][1]))
            for k in range(grid_index + 1, grid_index + dice + 1):
                # 当索引k超过棋子格子数时，索引k取余棋子格子数
                k = k % len(grids)
                x = (moved_chess[j][0] - grids[k][0]) / 35
                y = (moved_chess[j][1] - grids[k][1]) / 35
                while moved_chess[j][0] != grids[k][0] or moved_chess[j][1] != grids[k][1]:
                    if (-1 <= moved_chess[j][0] - grids[k][0] <= 1 and x != 0) or \
                            (-1 <= moved_chess[j][1] - grids[k][1] <= 1 and y != 0):
                        moved_chess[j][0] = grids[k][0]
                        moved_chess[j][1] = grids[k][1]
                    else:
                        moved_chess[j][0] = moved_chess[j][0] - x
                        moved_chess[j][1] = moved_chess[j][1] - y
                    draw_board()
                    pygame.display.flip()
                    time.sleep(0.02)
                # 判断棋子是否在终点门口
                if (moved_chess[j][0], moved_chess[j][1]) == end_grids[color][0] and k != grid_index + dice + 1:
                    for e in range(1, grid_index + dice + 1 - k):
                        x = (moved_chess[j][0] - end_grids[color][e][0]) / 35
                        y = (moved_chess[j][1] - end_grids[color][e][1]) / 35
                        while moved_chess[j][0] != end_grids[color][e][0] or moved_chess[j][1] != end_grids[color][e][1]:
                            if (-1 <= moved_chess[j][0] - end_grids[color][e][0] <= 1 and x != 0) or (
                                    -1 <= moved_chess[j][1] - end_grids[color][e][1] <= 1 and y != 0):
                                moved_chess[j][0] = end_grids[color][e][0]
                                moved_chess[j][1] = end_grids[color][e][1]
                            else:
                                moved_chess[j][0] = moved_chess[j][0] - x
                                moved_chess[j][1] = moved_chess[j][1] - y
                            draw_board()
                            pygame.display.flip()
                            time.sleep(0.02)
                    break
        # 判断棋子是否在起始格子中
        elif (moved_chess[j][0], moved_chess[j][1]) in start_pos:
            x = (moved_chess[j][0] - start_pos_first[color][0]) / 35
            y = (moved_chess[j][1] - start_pos_first[color][1]) / 35
            while moved_chess[j][0] != start_pos_first[color][0] or moved_chess[j][1] != start_pos_first[color][1]:
                if (-1 <= moved_chess[j][0] - start_pos_first[color][0] <= 1 and x != 0) or (
                        -1 <= moved_chess[0][1] - start_pos_first[color][1] <= 1 and y != 0):
                    moved_chess[j][0] = start_pos_first[color][0]
                    moved_chess[j][1] = start_pos_first[color][1]
                else:
                    moved_chess[j][0] = moved_chess[j][0] - x
                    moved_chess[j][1] = moved_chess[j][1] - y
                draw_board()
                pygame.display.flip()
                time.sleep(0.02)
            grid_index = grids.index((moved_chess[j][0], moved_chess[j][1]))
            for k in range(grid_index + 1, grid_index + dice):
                k = k % len(grids)
                x = (moved_chess[j][0] - grids[k][0]) / 35
                y = (moved_chess[j][1] - grids[k][1]) / 35
                while moved_chess[j][0] != grids[k][0] or moved_chess[j][1] != grids[k][1]:
                    if (-1 <= moved_chess[j][0] - grids[k][0] <= 1 and x != 0) or \
                            (-1 <= moved_chess[j][1] - grids[k][1] <= 1 and y != 0):
                        moved_chess[j][0] = grids[k][0]
                        moved_chess[j][1] = grids[k][1]
                    else:
                        moved_chess[j][0] = moved_chess[j][0] - x
                        moved_chess[j][1] = moved_chess[j][1] - y
                    draw_board()
                    pygame.display.flip()
                    time.sleep(0.02)
    else:
        x = (moved_chess[j][0] - start_pos[color][0]) / 6
        y = (moved_chess[j][1] - start_pos[color][1]) / 6
        while moved_chess[j][0] != start_pos[color][0] or moved_chess[j][1] != start_pos[color][1]:
            if (-1 <= moved_chess[j][0] - start_pos[color][0] <= 1 and x != 0) or (
                    -1 <= moved_chess[0][1] - start_pos[color][1] <= 1 and y != 0):
                moved_chess[j][0] = start_pos[color][0]
                moved_chess[j][1] = start_pos[color][1]
            else:
                moved_chess[j][0] = moved_chess[j][0] - x
                moved_chess[j][1] = moved_chess[j][1] - y
            draw_board()
            pygame.display.flip()
            time.sleep(0.02)
        # 起飞状态改为True
        moved_chess[j][2] = True
    # 飞行前或跳跃前碰撞
    check_collision(j)
    # 处理飞行情况
    fly_chess(j)
    # 飞行后或跳跃前碰撞
    check_collision(j)
    # 处理跳跃情况
    jump_chess(j)
    # 跳跃后碰撞
    check_collision(j)
    # 处理跳跃后飞行情况
    fly_chess(j)
    # 飞行后碰撞
    check_collision(j)
    # 判断棋子是否到达终点
    arrived_end(j)
    # 更改为掷骰子阶段
    stage = 0
    # 游戏轮数加1
    if dice != 6:
        game_round += 1


def arrived_end(j):
    """到达终点"""
    color = game_round % 4
    if (chess[color][j][0], chess[color][j][1]) == end_grids[color][-1]:
        x = (end_grids[color][-1][0] - row_chess_pos[color][j][0]) / 40
        y = (end_grids[color][-1][1] - row_chess_pos[color][j][1]) / 40
        while chess[color][j][0] != row_chess_pos[color][j][0] or chess[color][j][1] != row_chess_pos[color][j][1]:
            if (-1 <= chess[color][j][0] - row_chess_pos[color][j][0] <= 1 and x != 0) or (
                    -1 <= chess[color][j][1] - row_chess_pos[color][j][1] <= 1 and y != 0):
                chess[color][j][0] = row_chess_pos[color][j][0]
                chess[color][j][1] = row_chess_pos[color][j][1]
            else:
                chess[color][j][0] = chess[color][j][0] - x
                chess[color][j][1] = chess[color][j][1] - y
            draw_board()
            pygame.display.flip()
            time.sleep(0.02)
        chess[color][j][3] = True


def check_collision(j):
    """检测碰撞"""
    color = game_round % 4
    for t in range(1, 4):
        for g in range(4):
            if chess[(color + t) % 4][g][0] == chess[color][j][0] and chess[(color + t) % 4][g][1] == chess[color][j][1]:
                x = (chess[(color + t) % 4][g][0] - row_chess_pos[(color + t) % 4][g][0]) / 40
                y = (chess[(color + t) % 4][g][1] - row_chess_pos[(color + t) % 4][g][1]) / 40
                while chess[(color + t) % 4][g][0] != row_chess_pos[(color + t) % 4][g][0] or\
                        chess[(color + t) % 4][g][1] != row_chess_pos[(color + t) % 4][g][1]:
                    if (-1 <= chess[(color + t) % 4][g][0] - row_chess_pos[(color + t) % 4][g][0] <= 1 and x != 0) or \
                            (-1 <= chess[(color + t) % 4][g][1] - row_chess_pos[(color + t) % 4][g][1] <= 1 and y != 0):
                        chess[(color + t) % 4][g][0] = row_chess_pos[(color + t) % 4][g][0]
                        chess[(color + t) % 4][g][1] = row_chess_pos[(color + t) % 4][g][1]
                    else:
                        chess[(color + t) % 4][g][0] = chess[(color + t) % 4][g][0] - x
                        chess[(color + t) % 4][g][1] = chess[(color + t) % 4][g][1] - y
                    draw_board()
                    pygame.display.flip()
                    time.sleep(0.02)
                chess[(color + t) % 4][g][2] = False


def jump_chess(j):
    """跳跃"""
    color = game_round % 4
    if (chess[color][j][0], chess[color][j][1]) in grids and\
            (chess[color][j][0], chess[color][j][1]) not in end_grids[color]:
        grid_index = grids.index((chess[color][j][0], chess[color][j][1]))
        if grid_index % 4 == (color + 1) % 4:
            # 格子颜色与棋子颜色相同，则进行跳跃
            x = (chess[color][j][0] - grids[(grid_index + 4) % len(grids)][0]) / 40
            y = (chess[color][j][1] - grids[(grid_index + 4) % len(grids)][1]) / 40
            while chess[color][j][0] != grids[(grid_index + 4) % len(grids)][0] or\
                    chess[color][j][1] != grids[(grid_index + 4) % len(grids)][1]:
                if (-1 <= chess[color][j][0] - grids[(grid_index + 4) % len(grids)][0] <= 1 and x != 0) or \
                        (-1 <= chess[color][j][1] - grids[(grid_index + 4) % len(grids)][1] <= 1 and y != 0):
                    chess[color][j][0] = grids[(grid_index + 4) % len(grids)][0]
                    chess[color][j][1] = grids[(grid_index + 4) % len(grids)][1]
                else:
                    chess[color][j][0] = chess[color][j][0] - x
                    chess[color][j][1] = chess[color][j][1] - y
                draw_board()
                pygame.display.flip()
                time.sleep(0.02)


def fly_chess(j):
    """飞行"""
    color = game_round % 4
    if (chess[color][j][0], chess[color][j][1]) in grids:
        grid_index = grids.index((chess[color][j][0], chess[color][j][1]))
        if (grid_index - 17 >= 0 and (grid_index - 17) / 13 == color) or \
                (grid_index - 17 < 0 and (grid_index + 52 - 17) / 13 == color):
            x = (chess[color][j][0] - grids[(grid_index + 12) % len(grids)][0]) / 40
            y = (chess[color][j][1] - grids[(grid_index + 12) % len(grids)][1]) / 40
            while chess[color][j][0] != grids[(grid_index + 12) % len(grids)][0] or\
                    chess[color][j][1] != grids[(grid_index + 12) % len(grids)][1]:
                if (-1 <= chess[color][j][0] - grids[(grid_index + 12) % len(grids)][0] <= 1 and x != 0) or \
                        (-1 <= chess[color][j][1] - grids[(grid_index + 12) % len(grids)][1] <= 1 and y != 0):
                    chess[color][j][0] = grids[(grid_index + 12) % len(grids)][0]
                    chess[color][j][1] = grids[(grid_index + 12) % len(grids)][1]
                else:
                    chess[color][j][0] = chess[color][j][0] - x
                    chess[color][j][1] = chess[color][j][1] - y
                draw_board()
                pygame.display.flip()
                time.sleep(0.02)


def check_win():
    """胜利条件"""
    for c in range(4):
        num = 0
        for w in range(4):
            if chess[c][w][3]:
                num += 1
        if num == 4:
            print("恭喜玩家%d获胜" % (c + 1))


# 主循环
while True:
    # 检测胜利条件
    check_win()
    for event in pygame.event.get():
        # 检测窗口关闭事件
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        # 检测键盘事件
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_p:
                # 退出游戏
                pygame.quit()
                # 退出程序
                sys.exit()
            elif event.key == pygame.K_r:
                draw_dice()
        # 检测鼠标点击事件
        if event.type == pygame.MOUSEBUTTONDOWN:
            # 掷骰子阶段
            if stage == 0:
                x, y = pygame.mouse.get_pos()
                if 50 <= x <= 150 and 630 <= y <= 680:
                    draw_dice()
            # 移动棋子阶段
            elif stage == 1:
                j = choose_chess(pygame.mouse.get_pos())
                if j is not None:
                    move_chess(j)
    # 绘制棋盘
    draw_board()
    # 设置帧率
    clock.tick(60)
    # 刷新
    pygame.display.flip()

